﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Groups
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Groups;
    using System.Xml.Linq;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.SiteEvents;
    public partial class CreateGroup : GroupsPageBase
    {
        Guid newGroupId;
        protected void Page_Init(object sender, EventArgs e)
        {
            string action = Request.QueryString["a"];
            if (action == null || action == "c")
            {
                FormView1.DefaultMode = FormViewMode.Insert;
            }
            else if (action == "e" && Request.QueryString["id"] != null)
            {
                FormView1.DefaultMode = FormViewMode.Edit;
            }
            FormView1.ItemInserting += new FormViewInsertEventHandler(FormView1_ItemInserting);
            FormView1.ItemInserted += new FormViewInsertedEventHandler(FormView1_ItemInserted);
            FormView1.ItemUpdating += new FormViewUpdateEventHandler(FormView1_ItemUpdating);
            FormView1.ItemUpdated += new FormViewUpdatedEventHandler(FormView1_ItemUpdated);
            LinqDataSource1.Inserted += new EventHandler<LinqDataSourceStatusEventArgs>(LinqDataSource1_Inserted);
        }

        void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            if (e.Exception == null)
            {
                Sc_Group g = (Sc_Group)e.Result;
                dataContext.AddNewPostCategory(g.GroupName, g.GroupName, (int)EntityType.Group, g.GroupId);
                GroupOperations.AddUserToGroup(Profile.UserId, g.GroupId, GroupRole.Owner, true);
                
                XElement cxt = new XElement("Event",
                    new XElement("GroupName", g.GroupName),
                    new XElement("GroupId", g.GroupId),
                    new XElement("GroupCategory", g.GroupCategory),
                    new XElement("Creator", g.OwnerUserId));

                if (g.Privacy != (int)JoinGroupPrivacy.None)
                {
                    SiteEventOperations.AddEvent("Groups", "NewGroup", "Groups", Profile.UserId, cxt, true, null);
                }
                SiteEventOperations.AddEvent("Groups", "NewGroup", "Groups", Profile.UserId, cxt, true, g.GroupId);

                newGroupId = g.GroupId;
                
            }
            else
            {
                SetTopMostMessage("服务器错误，请重试。", true);
            }
        }

        void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            if (e.Exception == null)
            {
                SetTopMostMessage("圈子信息已更新。", true);
                RefreshPage();
            }
            else
            {
                SetTopMostMessage("服务器错误，请重试。", true);
            }
        }

        void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            
        }

        void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            e.Values["Approved"] = true;
            e.Values["CreateDate"] = DateTime.Now;
            e.Values["GroupAdmin"] = new XElement("Admins",
                new XElement("Admin",
                    new XAttribute("UserId", Profile.UserId)));
        }

        void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.Exception == null)
            {
                SetTopMostMessage("成功组建了一个圈子。", true);
                Response.Redirect("Manage/Avatar.aspx?id=" + newGroupId);
            }
        }

        void AddNewGroupSiteEvent()
        {

        }
    }
}
